﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;

using HK2009.BusinessFacade;
using HK2009.EntityManage;
using HK2009.Common;
using HK2009.EntityQuery;
using System.Threading;

namespace HK2009.BusinessFacadeHandler
{
    public class SystemHandler:ISystemHandler
    {
        UsersManage userManage = new UsersManage();
        DataBase db = new DataBase();
        Sy_ClientManage clientManage = new Sy_ClientManage();
        Sy_PlaceManage placeManage = new Sy_PlaceManage();
        BN_DriverManage driverManage = new BN_DriverManage();

        SystemConfigManage sysManage = new SystemConfigManage();

        /// <summary>
        /// 判断当前用户有没有输入的权限
        /// </summary>
        /// <returns></returns>
        public bool CheckPermission(int userID, int purviewID)
        {
            StringBuilder sb = new StringBuilder();
            bool istrue = false;
            UsersQuery query = userManage.GetUsersQuery(userID);
            if (query != null)
            {
                string roleIDs = query.Roles;
                if (roleIDs != string.Empty)
                {
                   
                    string strSql = "select * from Roles where RoleID in(" + roleIDs + ")";
                    DataTable dtRoles = db.RunSql(strSql);
                    if (dtRoles != null && dtRoles.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtRoles.Rows.Count; i++)
                        {
                            sb.Append(dtRoles.Rows[i]["RolePurview"].ToString() + ",");
                        }
                    }

                    string permission = sb.ToString();
                    if (permission.Length > 0)
                    {
                        permission = permission.Substring(0, permission.Length - 1);

                        
                        //判断用户有没有输入的权限
                        if (permission.Contains(purviewID.ToString()))
                        {
                            istrue = true;
                        }                                             
                    }
                }
            }
            return istrue;
        }


        public  string GetLoginUserPermissions()
        {
            if (System.Web.HttpContext.Current.Session["UserPermissions"] == null)
            {
                LoadLoginUserPermissions();
                
            }

            return System.Web.HttpContext.Current.Session["UserPermissions"].ToString();
        }

        public string GetLoginUserPermissions(bool getRoleCode)
        {
            if (!getRoleCode)
            {
                return GetLoginUserPermissions();
            }

            if (System.Web.HttpContext.Current.Session["UserPermissionsCode"] == null)
            {
                LoadLoginUserPermissions();
            }

            if (System.Web.HttpContext.Current.Session["UserPermissionsCode"] != null)
                return System.Web.HttpContext.Current.Session["UserPermissionsCode"].ToString();
            else
                return "";
        }

        private void LoadLoginUserPermissions()
        {
            StringBuilder sb = new StringBuilder();
            StringBuilder roleCodes = new StringBuilder();
            IRolePurviewHandler userManage = new RolePurviewHandler();
            UsersQuery query = userManage.GetUsers(int.Parse(System.Web.HttpContext.Current.Session["UserID"].ToString()));
            if (query != null)
            {
                string roleIDs = query.Roles;
                if (roleIDs != string.Empty)
                {
                    string strSql = "select * from Roles where RoleID in(" + roleIDs + ")";
                    DataTable dtRoles = db.RunSql(strSql);
                    if (dtRoles != null && dtRoles.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtRoles.Rows.Count; i++)
                        {
                            sb.Append(dtRoles.Rows[i]["RolePurview"].ToString() + ",");
                            roleCodes.Append(dtRoles.Rows[i]["RoleCode"].ToString() + ",");
                        }
                    }

                    string result = sb.ToString();
                    if (result.Length > 0)
                    {
                        result = result.Substring(0, result.Length - 1);
                        System.Web.HttpContext.Current.Session["UserPermissions"] = result;
                        result = roleCodes.ToString();
                        result = result.Substring(0, result.Length - 1);
                        System.Web.HttpContext.Current.Session["UserPermissionsCode"] = result;
                    }
                }
            }

        }

        public string GetClient(object id)
        {
            string name = string.Empty;
            Sy_ClientQuery query = clientManage.GetSy_ClientQuery(int.Parse(id.ToString()));
            if (query != null)
            {
                if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("en"))
                {
                    name = query.ENGShortName;
                }
                else if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("zh-hk"))
                {
                    name = query.HKShortName;
                }
                else
                {
                    name = query.CNShortName;
                }
            }
            return name;
        }

        public string GetPlace(object id)
        {
            
            string name = string.Empty;
            if (id != null && id.ToString() != string.Empty)
            {
                Sy_PlaceQuery query = placeManage.GetSy_PlaceQuery(int.Parse(id.ToString()));
                if (query != null)
                {
                    if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("en"))
                    {
                        name = query.ENGName;
                    }
                    else if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("zh-hk"))
                    {
                        name = query.HKName;
                    }
                    else
                    {
                        name = query.Name;
                    }
                }
            }
            return name;
        }

        public string GetUserName(object userID)
        {
            int user = 0;
            int.TryParse(userID.ToString(), out user);
            string userName = string.Empty;
            UsersQuery query = userManage.GetUsersQuery(user);
            if (query != null)
            {
                userName = query.LoginID;
            }
            return userName;
        }

        public string GetDriver(object id)
        {
            string name = string.Empty;
            BN_DriverQuery query = driverManage.GetBN_DriverQuery(int.Parse(id.ToString()));
            if (query != null)
            {
                name = query.Name;
            }
            return name;
        }

        public SystemConfigQuery GetConfig(string configName)
        {
            SqlParameter[] parms = new SqlParameter[1];
            parms[0] = new SqlParameter("@ConfigName",configName);

            SystemConfigQuery[] querys = sysManage.GetSystemConfigQueryList("ConfigName=@ConfigName",parms);
            if (querys != null && querys.Length > 0)
            {
                return querys[0];
            }
            else
            {
                return null;
            }

        }

        public bool UpdateConfig(SystemConfigQuery query)
        {
            return sysManage.UpdateSystemConfig(query);
        }


        /// <summary>
        /// 判断用户是否是公司用户2
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public bool IsCompanyRoles2()
        {
            string roleCode = GetLoginUserPermissions(true);
            return roleCode.Contains("CPMUSER2");
            
        }



    }
}
